package some.coupon.admin.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import some.coupon.admin.domain.ACouponCommand;

public class RegisterCouValidator implements Validator{

	@Override
	public boolean supports(Class<?> clazz) {
		return ACouponCommand.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "cou_id", "required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "cou_name", "required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "file", "required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "count", "required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "use_point", "required");
		
		ACouponCommand aCouponCommand = (ACouponCommand)target;
		
		if(aCouponCommand.getFile() == null || aCouponCommand.getFile().isEmpty())
			errors.rejectValue("file","required");
			
		if(aCouponCommand.getCount() <= 0)
			errors.rejectValue("count", "numberZero");
		
		if(aCouponCommand.getUse_point() <= 0)
			errors.rejectValue("use_point", "numberZero");
	}
	
	
}
